package gemini.castor.ui.client.page.content.customer.customerlist.list;

import gemini.basic.model.Customer;
import gemini.castor.ui.client.ServiceCatalog;
import gemini.castor.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.castor.ui.client.mvp.single.CastorWidgetDisplay;
import gemini.castor.ui.client.mvp.single.CastorWidgetPresenter;
import gemini.castor.ui.client.page.RootEvent;
import gemini.castor.ui.client.page.content.customer.customerlist.CustomerEvent;
import gemini.castor.ui.client.page.content.customer.customerlist.widget.CustomerTableEvent;
import gemini.castor.ui.client.page.content.customer.customerlist.widget.CustomerTableHandler;
import gemini.castor.ui.client.page.content.customer.customerlist.widget.CustomerTableWidget;
import gemini.castor.ui.client.utils.PlaceConstants;

import java.util.List;

import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.inject.Inject;

public class CustomerListPresenter extends
		CastorWidgetPresenter<CustomerListPresenter.Display> {

	@Inject
	public CustomerListPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends CastorWidgetDisplay {

		CustomerTableWidget getCustomerTableWidget();

		void initCustom();

		void showCustomers(List<Customer> customers);

	}

	private String distributorCode;


	@Override
	public void setInputObject(Object ob) {
			distributorCode = (String) ob;
	}

	public static final Place PLACE = new Place(PlaceConstants.PLACE_EVENTS);

	private void registerCustomerHandler() {
		registerHandler(display.getCustomerTableWidget().addEventsActionHandler(
				new CustomerTableHandler() {
					
					@Override
					public void onEdit(CustomerTableEvent event) {
						CustomerEvent newEvent = new CustomerEvent(
								CustomerEvent.ForwardType.EDIT);
						newEvent.setCustomer(event.getCustomer());
						localEventBus.fireEvent(newEvent);
					}
					
					@Override
					public void onDelete(CustomerTableEvent event) {
						eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
						ServiceCatalog.getCustomerService().deleteCustomer(event.getCustomer().getId(),
								new AbstractAsyncCallback<Void>() {

									@Override
									public void onSuccess(Void result) {
										eventBus.fireEvent(new RootEvent(
												RootEvent.ForwardType.UN_MASK));
										localEventBus.fireEvent(new CustomerEvent(
												CustomerEvent.ForwardType.BACK_TO_LIST));
									}
								});
					}
					
					@Override
					public void onAdd(CustomerTableEvent event) {
						localEventBus.fireEvent(new CustomerEvent(
								CustomerEvent.ForwardType.NEW));
						
					}
				}));
	}

	@Override
	protected void onBind() {
		registerCustomerHandler();
		eventBus.fireEvent(new RootEvent(RootEvent.ForwardType.MASK));
		ServiceCatalog.getCustomerService().getCustomerByDistributorCode(
				distributorCode, new AbstractAsyncCallback<List<Customer>>() {

					@Override
					public void onSuccess(List<Customer> result) {
						eventBus.fireEvent(new RootEvent(
								RootEvent.ForwardType.UN_MASK));
						if (result != null && !result.isEmpty()) {
							display.initCustom();
							display.showCustomers(result);
						}
					}
				});
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	@Override
	public void onUnbind() {
	}
}
